//获取应用实例
const app = getApp()

Page({
  // 页面的初始数据
  data: {
    array: [
      '陕',
      '京',
      '津',
      '沪',
      '冀',
      '川',
      '粤',
      '豫',
      '渝',
      '贵',
      '闽',
      '苏',
      '浙',
      '赣',
      '云',
      '辽',
      '黑',
      '湘',
      '皖',
      '鲁',
      '新',
      '鄂',
      '桂',
      '甘',
      '晋',
      '蒙',
      '吉',
      '青',
      '藏',
      '琼',
      '宁'
    ],
    user: null,
    code: null,
    provincecode : 0,
    province: '陕',
    agree: false,
    carnumber: null,
    phone: null,
    top : [],
    bottom : [],
  },
  bindPickerChange: function (e) {
    let province = this.data.array[e.detail.value]
    this.setData({ provincecode: e.detail.value, province: province })
  },
  agree: function (e) {
    if (e.detail.value.length > 0) {
      this.setData({ agree: true })
    } else {
      this.setData({ agree: false })
    }
  },
  onShow: function () {
    let _this = this;
    app.GET('api/amber/index', {}, function (res) {
      let top = []
      let bottom = []
      res.data.forEach(d => {
        if (d.location == 1) {
          top.push(d)
        } else if (d.location == 2) {
          bottom.push(d)
        }
      })
      _this.setData({ top: top, bottom: bottom })
    })
    app.refreshUserInfo(function (user) {
      app.globalData.onLogin && app.globalData.onLogin(user)
      _this.setData({
        user: user,
        provincecode: _this.data.provincecode || user.provincecode || 0,
        province: _this.data.province || user.province || '陕',
        code: _this.data.code || user.code,
        carnumber: _this.data.carnumber || user.carnumber,
        phone: _this.data.phone || user.phone,
        agree: _this.data.agree || true,
      })
    })
    wx.setNavigationBarTitle({ title: '绑定车牌' });
  },
  onLoad : function(options){
    console.log('1.1.1')
    this.data.code = options.code
  },
  carnumber: function (e) {
    if (e.detail.value.length < 10){
      this.setData({ carnumber: e.detail.value })
    }else{
      this.setData({ carnumber: this.data.carnumber })
    }
  },
  phone: function (e) {
    this.setData({ phone: e.detail.value })
  },
  bindCartCode: function (e) {
    let _this = this;
    let math = /^1(?:3\d|4[4-9]|5[0-35-9]|6[67]|7[013-8]|8\d|9\d)\d{8}$/
    let phone = _this.data.phone
    if (!math.test(phone)) {
      app.showError('您输入的手机号码格式不正确')
      return
    }
    let bindData = {
      provincecode: _this.data.provincecode,
      province: _this.data.province,
      carnumber: _this.data.carnumber,
      phone: phone,
      code: _this.data.code,
      formId : e.detail.formId
    }
    app.getUserInfoAndLogin(function (user) {
      bindData.user = user.id
      wx.showLoading({ title: "正在提交", mask: true });
      app.POST('api/user/bind', bindData, function (res) {
        app.showSuccess('绑定成功', function () {
          app.getUserInfoAndLogin(function () { })
          console.log(user.status)
          if (user.status != 1){
            console.log('pay')
            wx.navigateTo({ url: '/pages/home/pay' })
          }else{
            wx.navigateBack() 
          }
        })
      })
    })
  },
  updateCartCode: function () {
    let _this = this;
    wx.scanCode({
      onlyFromCamera: true,
      success: (res) => {
        if (res.result && res.result.startsWith(app.globalData.PROFILE_RECOVER_FLAG) &&
          res.result > app.globalData.PROFILE_RECOVER_FLAG + 6) {
          let code = res.result.substring(app.globalData.PROFILE_RECOVER_FLAG.length + 6)
          app.GET('api/user/code', { code: code }, function (user) {
            if (user && user.data && user.data.id && user.data.id != _this.data.user.id) {
              app.showError('当前二维码已经被绑定')
            } else {
              let math = /^1(?:3\d|4[4-9]|5[0-35-9]|6[67]|7[013-8]|8\d|9\d)\d{8}$/
              let phone = _this.data.phone
              if (!math.test(phone)) {
                app.showError('您输入的手机号码格式不正确')
                return
              }
              let bindData = {
                provincecode: _this.data.provincecode,
                province: _this.data.province,
                carnumber: _this.data.carnumber,
                phone: phone,
                code: code
              }
              app.getUserInfoAndLogin(function (user) {
                bindData.user = user.id
                wx.showLoading({ title: "正在提交", mask: true });
                app.POST('api/user/bind', bindData, function (res) {
                  app.getUserInfoAndLogin(function () { })
                  app.showSuccess('绑定成功', function () { wx.navigateBack() })
                })
              })
            }
          })
        } else {
          app.showError('您扫描的二维码似乎不是本公司的产品')
        }
      }
    })
  },
  payPage : function(){
    console.log('payPage')
    wx.navigateTo({ url: '/pages/home/pay' })
  }
})